package com.xxl.job.admin.common.utils;

import com.xxl.job.admin.common.config.ApplicationLoadConfig;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * 时间工具类
 *
 * @author : zyc
 * @version : 1.0
 * @create : 2023/09/28 14:22
 **/
public class TimeUtils extends org.apache.commons.lang3.time.DateUtils {

    public static String YYYY_MM_DD = "yyyy-MM-dd";

    public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

    private static String[] parsePatterns = {
            "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
            "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
            "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};


    /**
     * 获取当前日期, 默认格式为yyyy-MM-dd
     *
     * @return String
     */
    public static String getDate() {
        return dateTimeNow(YYYY_MM_DD);
    }

    public static final String dateTimeNow(final String format) {
        return parseDateToStr(format, TimeUtils.getCountryNowTime());
    }

    public static final String parseDateToStr(final String format, final Date date) {
        return new SimpleDateFormat(format).format(date);
    }

    /**
     * 根据国家时区获取当前时间 (年月日)
     *
     * @author: zyc
     * @return: java.util.Date getCountryNowTime
     * @date: 2022/8/25
     */
    public static Date getCountryNowTimeYMD() {
        SimpleDateFormat sdf = new SimpleDateFormat(YYYY_MM_DD);
        Calendar calendar = Calendar.getInstance();
        sdf.setTimeZone(TimeZone.getTimeZone(ApplicationLoadConfig.getNationalTimeZone()));
        String time = sdf.format(calendar.getTime());
        return TimeUtils.parseDate(time);
    }

    /**
     * 根据国家时区获取当前时间 (年月日时分秒)
     * 注意：xxl-job-admin模块中原获取系统时间 new Date() 改为 TimeUtils.getCountryNowTime()
     *
     * @author: zyc
     * @return: java.util.Date getCountryNowTime
     * @date: 2023/09/28
     */
    public static Date getCountryNowTime() {
        SimpleDateFormat sdf = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
        Calendar calendar = Calendar.getInstance();
        sdf.setTimeZone(TimeZone.getTimeZone(ApplicationLoadConfig.getNationalTimeZone()));
        String time = sdf.format(calendar.getTime());
        return TimeUtils.parseDate(time);
    }

    /**
     * 根据国家时区获取当前时间 (年月日时分秒) + 指定的毫秒数
     * 注意：xxl-job-admin模块中原获取系统时间 改为 TimeUtils.getCountryNowTimeByMilliSecond()
     *
     * @param milliSecond 毫秒数
     * @author: zyc
     * @return: java.util.Date getCountryNowTime
     * @date: 2023/09/28
     */
    public static Date getCountryNowTimeByMilliSecond(long milliSecond) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
        Calendar calendar = Calendar.getInstance();
        sdf.setTimeZone(TimeZone.getTimeZone(ApplicationLoadConfig.getNationalTimeZone()));
        String time = sdf.format(calendar.getTime());
        Date now = sdf.parse(time);
        return new Date(now.getTime() + milliSecond);
    }

    /**
     * 根据当前国家获取时间戳
     *
     * @return: long
     * @author: zyc
     * @date: 2023/10/10 9:15
     */
    public static long getCowCurrentTimeMillis() {
        Date nowTime = getCountryNowTime();
        if (nowTime == null) {
            return 0;
        }
        return nowTime.getTime();
    }

    /**
     * 日期型字符串转化为日期 格式
     */
    public static Date parseDate(Object str) {
        if (str == null) {
            return null;
        }
        try {
            return parseDate(str.toString(), parsePatterns);
        } catch (ParseException e) {
            return null;
        }
    }
}
